﻿using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Webapi.EFCore.IRepository;
using Webapi.EFCore.Model;

namespace Webapi.EFCore.Repository
{
    /// <summary>
    /// 工作单元
    /// </summary>
    [Service]
    public class UnitOfWork : IUnitOfWork
    {
        #region  构造函数、私有变量
        private readonly EFCoreDbContext myDbContext;

        public UnitOfWork(EFCoreDbContext dbContext)
        {
            this.myDbContext = dbContext;
        }

        #endregion

        /// <summary>
        /// 获取上下文
        /// </summary>
        /// <returns></returns>
        public DbContext GetDbContext()
        {
            return myDbContext;
        }

        /// <summary>
        /// 批量异步提交
        /// </summary>
        /// <returns></returns>
        public async Task<int> SaveChangesAsync()
        {
            return await myDbContext.SaveChangesAsync();
        }

        /// <summary>
        /// 批量同步提交
        /// </summary>
        /// <returns></returns>
        public int SaveChanges()
        {
            return myDbContext.SaveChanges();
        }
    }
}
